<?php

	require_once(dirname(__FILE__) . "/../core/safeParam.php");

	if( (isset($_GET['function'])) && (isset($_GET['cid']))){
		$function = $_GET['function'];
		$cid      = $_GET['cid'];
	}

	switch ($function) {
		default:
		if (!isset($cid)) {
			echo '
			<div style="margin-left:20px; margin-bottom:20px;">
				<h2>Opciones Globales</h2>
				<p align="center" style="font-weight:normal;">
					Agregue opciones para que todos sus productos las incorporen.
				</p>
			</div>	
			
			<div style="margin-left:20px; margin-bottom:10px;">
				<a href="index.php?edit=global-options&function=add">
					<img src="add.png" border="0">&nbsp;&nbsp;Agregar Opciones Globales
				</a>
			</div>
			 
        	<p align="center">	
        	<table cellspacing="2" cellpadding="2" border="0" class="table">
				<tr>
					<th>ID</th>
            	<th>Nombre</th>
            	<th>Acción</th>
          	</tr>';
      		
      		$results = $database->select("SELECT * FROM options_set WHERE rid = " . ARID . "  AND global = 1 order by name ");
      		
      		if ($results == true) {
      			for ($i = 0; $i < count($results); $i++) {
      				echo '<tr>
                  <td>' . $results[$i]['id'] . '  </td>
                  
                  <td>
                    <a href="index.php?edit=global-options&function=edit&cid=' . $results[$i]['id'] . '">' . $results[$i]['name'] . '  </a>
                  </td>
                  
                  <td>
                    <a href="index.php?edit=global-options&function=edit&cid=' . $results[$i]['id'] . '"><img src="application_edit.png"  border="0"> Edit </a> 
                    <a href="../functions/global-options.php?function=delete&id=' . $results[$i]['id'] . '"><img src="delete.png"  border="0"> Delete </a> 
                  </td>
					</tr>';
        			} //$i = 0; $i < count($results); $i++
      		} //$results == true
      		
      		echo '</table>';
    	} else {
    			$results = $database->select("SELECT * FROM options_set where id = $cid ");
?>
	<span>
		<a class="button" href="javascript:openModal('#add-option',200,400)">
			<img src="add.png" border="0">Agregar nueva Opción
		</a>
	</span> 
	<form action="../functions/global-options.php?function=edit" method="post">
		<input type="hidden" name="id" value="<?php  echo $results[0]['id']; ?>">
		<table  cellspacing="3" cellpadding="3"  class="table_form">
			<tr>
				<td>Nombre: </td>
          	<td><input type="text" name="name" value="<?echo $results[0]['name']; ?>"></td>
        </tr>
        
        <tr>
          <td>Cuantas opciones puede seleccionar el usuario<br /><em>(dejar en blanco para selecciones indefinidas)</em> </td>
          <td><input type="text" name="max_toppings" value="<?echo $results[0]['max_toppings']; ?>"></td>
        </tr>
        
        <tr>
          <td>¿Puede el usuario seleccionar múltiples opciones?</td>
          <td>
            <select name="multi">
              <option  value="1" <?php if ($results[0]['multi'] == 1) { echo '  selected'; } //$results[0]['multi'] == 1 ?>>Si</option>
              <option value="0"  <?php if ($results[0]['multi'] == 0) { echo '  selected'; } //$results[0]['multi'] == 0 ?>>No</option>
            </select>
          </td>
        </tr>
        
        <tr>
          <td>Asignar a las categorías (dejar en blanco para todas las categorías)</td>
          <td>
          	<select name="cid[]" multiple="multiple" style="height:200px;width:200px" >
          	<?php
              $cats       = $database->select("SELECT * FROM menu_cat where rid =  " . ARID . " order by name ");
              $cats_array = unserialize($results[0]['cid']);              
              for ($h = 0; $h < count($cats); $h++) {
                if (is_array($cats)) {
                  if (in_array($cats[$h]['id'], $cats_array)) {
                    $selected = 'selected="selected"';
                  } //in_array($cats[$h]['id'], $cats_array)
                  else {
                    $selected = '';
                  }
                } //is_array($cats)
                echo ' <option value="' . $cats[$h]['id'] . '" ' . $selected . '>' . $cats[$h]['name'] . '</option>';
              } //$h = 0; $h < count($cats); $h++
            ?>
				</select>
			</td>
		</tr>

    	<tr>
			<td colspan="2">
				<div align="center">
					<input type="submit" value="Guardar" style="padding:10px;">
				</div>
			</td>
    	</tr>  
	</table>
</form>

<div style="display:none">
	<div id="add-option" title="Add an option" class="popup">
		<form action="../functions/global-options.php?function=add-option" method="post">
			<input type="hidden" name="parent" value="<?php echo $results[0]['id']; ?>">
     		<table width="98%" border="0" cellspacing="6" cellpadding="6"  style="text-align:left">
      		<tr>
        			<td>Nombre</td>
        			<td><input type="text" name="name"></td>
      		</tr>
      		
      		<tr>
        			<td>Precio</td>
        			<td><input type="text" name="price"></td>
      		</tr>
      		
      		<tr>
        			<td>&nbsp;</td>
        			<td><input type="submit" value="Guardar"></td>
      		</tr>
    		</table>
		</form>
  </div>
</div>
                    
<?php
	if ($_GET['iid'] != "") {
		$iid     = $_GET['iid'];
		$results = $database->select("SELECT * FROM options_items where id = $iid ");
?>
		<span><a class="button" href="javascript:openModal('#add-option',200,400)"><img src="add.png" border="0"> Add new option</a>   </span> 
		<form action="../functions/global-options.php?function=edit-option" method="post">
			<input type="hidden" name="id" value="<?php echo $results[0]['id']; ?>">
     		<input type="hidden" name="parent" value="<?php  echo $_GET['cid']; ?>">
			<table width="98%" border="0" cellspacing="3" cellpadding="3"  class="table_form" style="text-align:left">
      		<tr>
        			<td>Nombre</td>
        			<td><input type="text" name="name" value="<?php  echo $results[0]['name']; ?>"></td>
      		</tr>
      		
      		<tr>
        			<td>Precio</td>
        			<td><input type="text" name="price" value="<?php  echo $results[0]['price']; ?>"></td>
      		</tr>
     
        		<tr>
        			<td></td>
        			<td><input type="submit" value="Guardar"></td>
      		</tr>
    		</table>
    	</form>
<?php
	} //$_GET['iid'] != ""
	else {
		$results = $database->select("SELECT * FROM options_items WHERE parent = " . $cid . " order by name ");
		echo '	
			<table cellspacing="2" cellpadding="2" border="0" class="table">
				<tr>
					<th>ID  </th>
					<th>Nombre</th>
              <th>Precio</th>
              <th>Acción</th>
            </tr>';
			for ($i = 0; $i < count($results); $i++) {
				echo '<tr>
				<td>' . $results[$i]['id'] . '  </td>
            <td><a href="index.php?edit=global-options&function=edit-option&iid=' . $results[$i]['id'] . '&cid=' . $_GET['cid'] . '">' . $results[$i]['name'] . '  </a></td>
            <td>' . $results[$i]['price'] . '  </td>
            
            <td>
            	<a href="index.php?edit=global-options&function=edit-option&iid=' . $results[$i]['id'] . '&cid=' . $_GET['cid'] . '"><img src="application_edit.png"  border="0"> Edit </a> 
            	<a href="../functions/global-options.php?function=delete-option&id=' . $results[$i]['id'] . '&parent=' . $_GET['cid'] . '"><img src="delete.png"  border="0"> Delete </a> 
            </td>
            </tr>';
			} //$i = 0; $i < count($results); $i++
        	
        	echo '</table>';
	}
}

break;

case "add":
?>

<form action="../functions/global-options.php?function=add" method="post">
  <input type="hidden" name="rid" value="<?php echo ARID; ?>" />
  <input type="hidden" name="id" value="<?php  echo $results[0]['id']; ?>">
  <table width="98%" border="0" cellspacing="3" cellpadding="3"  class="table_form" style="text-align:left">
    <tr>
      <td>Nombre</td>
      <td><input type="text" name="name" value="<?php echo $results[0]['name']; ?>"></td>
    </tr>
    
    <tr>
      <td>Cuantas opciones puede seleccionar el usuario<br /><em>(dejar en blanco para selecciones indefinidas)</em> </td>
      <td><input type="text" name="max_toppings" ></td>
    </tr>
    
    <tr>
      <td>¿Puede el usuario seleccionar múltiples opciones?</td>
      <td><select name="multi">
      <option value="1"  selected>Si</option>
      <option value="0" >No</option>
      </select></td>
    </tr>
    
    <tr>
      <td>Asignar a las categorías (dejar en blanco para todas las categorías)</td>
      <td>
        <select name="cid[]" multiple="multiple" style="height:200px;width:200px" >
          <?php
          $cats = $database->select("SELECT * FROM menu_cat where rid =  " . ARID . " order by name
                                                               ");
          for ($h = 0; $h < count($cats); $h++) {
            echo ' <option value="' . $cats[$h]['id'] . '" >' . $cats[$h]['name'] . '</option>';
          } //$h = 0; $h < count($cats); $h++
          ?>
        </select>
      </td>
    </tr>
    
    <tr>
		<td colspan="2">
			<div align="center">
				<input type="submit" value="Guardar" style="padding:10px;">
			</div>
		</td>
    </tr>
  </table>
</form>

	
<?php
    break;
} //$function
?>
